import Vue from 'vue'
import Vuex from 'vuex'
import http from '../api'
Vue.use(Vuex)

const store = new Vuex.Store({
    state:{
        userCartNum:'0',  // 购物车商品数量
        confirmAddress: '', // 确认订单修改收货地址
        userInfo:{},
        isLogin:false,
		location:{},
		classify:[],
		playList:[],
		login: true, 
    },
     getters:{
        getuserCartNum(state){
            return state.userCartNum
        },
        getconfirmAddress(state){
            return state.confirmAddress
        },
        getuserInfo(state){
            return state.userInfo
        },
		getLocation(state){
			return state.location
		},
		getClassify(state){
			return state.classify
		},
		getPlayList(state){
			return state.playList
		},
    },
    mutations:{
        /**
         *
         * 修改购物车商品数量
         * @param {any} state
         * @param {any} data
         */
        CartNum(state,data) {
            state.userCartNum = data
        },
		logout(state) {  
		            state.login = false 
		        },
				logins(state) {
				            state.login = true
				        },
		setPlayList(state,data){
			console.log(data)
			state.playList = data
		},
		setLocation(state,data) {
		    state.location = data
		},
		setClassify(state,data) {
		    state.classify = data
		},
        /**
         *  确认订单修改收货地址
         */
        editAddress(state,data = '') {
            state.confirmAddress = data
        },
        // 存储用户信息
        setUserInfo(state,data={}){
            state.userInfo= data
        },
        loginStatus(state,isLogin=false){
            state.isLogin = isLogin
        }
    },

    actions:{
        contentnum({commit},data){
            http.getgetnum(data).then(res=>{
                if(res.status == 200) {
                    commit('CartNum',res.data.countnum)
                    if(Number(res.data.countnum) > 0) {
                        uni.setTabBarBadge({
                            index: 3,
                            text: `${res.data.countnum}`
                        })
                    } else {
                        uni.removeTabBarBadge({
                            index: 3,
                        })
                    }
                }
            })

        }
    }
})

export default store
